﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using Newtonsoft.Json;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.weixin.Response
{
    public partial class KeywordRuleTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string op = Request["op"];
            if (!string.IsNullOrEmpty(op))
            {
                string data = "";
                if (Request["op"] == "LoadKeywordRuleTree")
                {
                    data = LoadKeywordRuleTree();
                }
                else if (Request["op"] == "AddKeywordRule")
                {
                    data = AddKeywordRule(Int32.Parse(Request["ParentId"]), Request["RuleTitle"]);
                }
                else if (Request["op"] == "RemoveKeywordRule")
                {
                    data = RemoveKeywordRule(Int32.Parse(Request["Id"]));
                }
                else if (Request["op"] == "UpdateKeywordRule")
                {
                    data = UpdateKeywordRule(Int32.Parse(Request["Id"]), Request["RuleTitle"]);
                }
                else if (Request["op"] == "SaveKeywordRuleDetail")
                {
                    data = SaveKeywordRuleDetail(Int32.Parse(Request["Id"]), Request["RequestKeyword"], Int32.Parse(Request["ResponseMaterialId"]), Request["FullMatch"]);
                }
                Response.Write(data);
                Response.End();
            }
        }

        public string LoadKeywordRuleTree()
        {
            IList<WxResponseRule> list = new List<WxResponseRule>();
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                list = session.CreateQuery("from WxResponseRule where Kind='KWTREE' and OwnerAccount=:acc")
                    .SetParameter("acc", UserKit.GetUserAccount())
                    .List<WxResponseRule>();
            }
            return JsonConvert.SerializeObject(list);
        }

        public string AddKeywordRule(int ParentId, string RuleTitle)
        {
            WxResponseRule rule = new WxResponseRule();
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                rule.IsAvailable = "1";
                rule.RequestType = "Keyword";
                rule.Kind = "KWTREE";
                rule.ParentId = ParentId;
                rule.RuleTitle = RuleTitle;
                rule.OwnerAccount = UserKit.GetUserAccount();
                session.SaveOrUpdate(rule);
                session.Flush();
            }
            return JsonConvert.SerializeObject(rule);
        }

        public string UpdateKeywordRule(int Id, string RuleTitle)
        {
            WxResponseRule rule = new WxResponseRule();
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                rule = session.Get<WxResponseRule>(Id);
                rule.RuleTitle = RuleTitle;
                rule.OwnerAccount = UserKit.GetUserAccount();
                session.SaveOrUpdate(rule);
                session.Flush();
            }
            return JsonConvert.SerializeObject(rule);
        }

        public void GetChildNodeIds(IList<WxResponseRule> list, int id, ref List<int> ids)
        {
            foreach (WxResponseRule type in list)
            {
                if (type.Id == id)
                {
                    ids.Add(type.Id);
                }
                if (type.ParentId == id)
                {
                    GetChildNodeIds(list, type.Id, ref ids);
                }
            }
        }

        public string RemoveKeywordRule(int Id)
        {
            string owner = UserKit.GetUserAccount();
            using (ISession session = SessionFactoryHelper.CreateSession())
            {

                IList<WxResponseRule> list = session.CreateQuery("from WxResponseRule where Kind='KWTREE' and OwnerAccount=:acc")
                    .SetParameter("acc", owner)
                    .List<WxResponseRule>();
                List<int> ids = new List<int>();

                GetChildNodeIds(list, Id, ref ids);

                session.CreateQuery("delete from WxResponseRule where Id in (:ids)")
                    .SetParameterList("ids", ids.ToArray())
                    .ExecuteUpdate();

                session.Flush();
            }
            return "ok";
        }

        public string SaveKeywordRuleDetail(int Id, string RequestKeyword, int ResponseMaterialId, string FullMatch)
        {
            WxResponseRule rule = null;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                rule = session.Get<WxResponseRule>(Id);
                rule.RequestKeyword = RequestKeyword;
                rule.ResponseMaterialId = ResponseMaterialId;
                rule.FullMatch = FullMatch;
                rule.OwnerAccount = UserKit.GetUserAccount();
                session.SaveOrUpdate(rule);
                session.Flush();
            }

            return JsonConvert.SerializeObject(rule);
        }
    }
}